package controller

import (
	"fmt"
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/organization"
	"gq_volunteer_platform/model/weixin"
	"sort"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/xuri/excelize/v2"
)

func Test(c *gin.Context) {

	var rq weixin.QRCodeGetReq
	rq.Scene = "aa=bb"
	rq.EnvVersion = "trial"
	rq.IsHyaline = true
	rq.Page = "pages/index/index"
	rq.Width = 46

	var by, err = rq.QRCodeGet()
	if err != nil {
		c.String(400, err.Error())
		return
	}

	c.Header("Content-Type", "image/jpeg")
	c.Writer.Write(by)
}

func Test2(c *gin.Context) {
	// 1. 打开模板文件
	f, err := excelize.OpenFile("temp.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}

	defer func() {
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	// 2. 填充数据（这里可以从请求参数或数据库获取）
	// f.SetCellValue("Sheet1", "B2", c.Query("name"))
	// f.SetCellValue("Sheet1", "C2", c.Query("age"))
	// f.SetCellValue("Sheet1", "D5", c.Query("department"))

	// 3. 设置响应头
	c.Header("Content-Type", "application/octet-stream")
	c.Header("Content-Disposition", "attachment; filename=export.xlsx")
	c.Header("Content-Transfer-Encoding", "binary")

	// 4. 写入响应
	if err := f.Write(c.Writer); err != nil {
		c.JSON(500, gin.H{"error": err.Error()})
	}

}

func Time(c *gin.Context) {
	c.String(200, time.Now().Format(time.DateTime))
}

func AddDep(c *gin.Context) {
	return
	var req []struct {
		ID     string `json:"id"`
		Pid    string `json:"pId"`
		Name   string `json:"name"`
		Parent string `json:"parent"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		c.String(400, err.Error())
		return
	}

	sort.Slice(req, func(i, j int) bool {
		var iPid, err = strconv.Atoi(req[i].Pid)
		if err != nil {
			c.String(400, err.Error())
		}
		jPid, err := strconv.Atoi(req[j].Pid)
		if err != nil {
			c.String(400, err.Error())
		}

		return iPid < jPid
	})

	// 加入数据库
	// for _, v := range req {
	// 	fmt.Println(v.Pid)
	// 	// 排除已加入组织
	// 	var f organization.Organization
	// 	if err := bean.MysqlDb.Where("`name`=?", v.Name).Find(&f).Error; err != nil {
	// 		bean.Log.Error(err)
	// 		c.String(500, "数据库错误")
	// 		return
	// 	}

	// 	if f.ID != 0 {
	// 		continue
	// 	}

	// 	var one organization.Organization
	// 	one.Name = v.Name
	// 	if err := bean.MysqlDb.Create(&one).Error; err != nil {
	// 		bean.Log.Error(err)
	// 		c.String(500, "数据库错误")
	// 		return
	// 	}
	// }
	// return
	// 增加关联关系
	for _, v := range req {
		// fmt.Println(v)
		if v.Parent == "" {
			continue
		}
		var one organization.Organization
		if err := bean.MysqlDb.Where("`name`=?", v.Name).First(&one).Error; err != nil {
			bean.Log.Warning(err)
			c.String(400, fmt.Sprintf("组织不存在id:%+v", v))
			return
		}

		var parent organization.Organization
		if err := bean.MysqlDb.Where("`name`=?", v.Parent).First(&parent).Error; err != nil {
			bean.Log.Warning(err)
			c.String(400, fmt.Sprintf("组织不存在id:%+v", v))
			return
		}

		var two organization.Organization
		two.ID = one.ID
		two.ParentId = parent.ID

		// if v.Name == "航空港区综合执法局学雷锋志愿服务大队" {
		// 	c.PureJSON(200, gin.H{
		// 		"v":   v,
		// 		"two": two,
		// 	})
		// 	return
		// }

		if err := bean.MysqlDb.Updates(&two).Error; err != nil {
			bean.Log.Error(err)
			c.String(500, "数据库错误")
			return
		}

	}

}
